package tcp_net.mail.pop3.commands;

import tcp_net.mail.MailBoxFileManager;
import tcp_net.mail.pop3.commands.debug.DebugInfo;
import java.util.List;
import java.util.ArrayList;

/**
	This class provides a defined command to be executed
	@author J. Okuno
*/
public class LIST extends POP3Command 
{
	/**
		A message number
	*/
	private int msg;
	
	/**
		Init the class
		@param m a message number
		@param man a mailbox file manager
	*/
	public LIST(int m, MailBoxFileManager man) 
	{ 
		super(man);
		msg = m; 
	}
	
	/**
		Init the class
		@param man a mailbox file manager
	*/
	public LIST(MailBoxFileManager man) 
	{ 
		this(-1, man);
	}
	
	/**
		This method executes a defined command
		@return a list of reply messages in a string format
	*/
	public List<String> execute() 
	{
		if(msg == -1)
		{
			int no_of_msgs = manager.getNoOfMessages();
			
			reply = new POP3ReplyMessage(true);
			reply_msgs.add(reply.execute().get(0));
			
			for(int i=1; i <= no_of_msgs; i++)
			{
				reply_msgs.add(i+" "+manager.getMessageSize(i-1));
			}
			reply_msgs.add(".");
			
			return reply_msgs;
		}
		
		//must write a response for all the messages 
		else if(msg > 0)
		{
			int size = manager.getMessageSize(msg);
			
			if(size>=0)
			{
				reply = new POP3ReplyMessage(true);
				reply_msgs.add(reply.execute().get(0)+" "+msg+" "+size);
				return reply_msgs;
			}
		}
		
		//an error case
		reply = new POP3ReplyMessage(false);
		reply_msgs.add(reply.execute().get(0));
		return reply_msgs;
	}
}